clear all 
set more off 
set maxvar 15000 
clear matrix


    use "$CensusData/input/Census_1910to2010_1pct_raw.dta", clear 
    
    tab year
    keep if age>=30 & age<=50 
    keep if bpl<100 //keeping US-born individuals (not including territories)
    
    * Fix weight so that it has mean 1 in each year
    gen weight_center=.
    levelsof year, local(decades)
    foreach x of local decades {
    
    sum perwt if year==`x'
        local weight_avg = r(mean)    
        replace weight_center = perwt/`weight_avg' if year==`x'
    }
    bysort year: sum weight_center
    
**-----------------------------------------------------*

    * Generate race x sex groups
    gen group=1 if sex==1 & race==1
    replace group=2 if sex==2 & race==1
    replace group=3 if sex==1 & race==2
    replace group=4 if sex==2 & race==2, 
    tab group [aw=perwt], m
    drop if group==.
    
    gen non_hh = gq==3 
    
    keep if birthyr>=1910 & birthyr<=1980
    
    collapse (mean) non_hh [aw=weight_center], by(year birthyr group)
    replace non_hh =non_hh * 100

    foreach x in 1 2 3 4 {
        if "`x'"=="1" local title "whitemen"
        if "`x'"=="2" local title "whitewomen"
        if "`x'"=="3" local title "blackmen"
        if "`x'"=="4" local title "blackwomen"
            
        #delimit ;
        twoway (line non_hh birthyr if group==`x' & year==1950, lcolor(black) lpat(dash) lwidth(0.4)) 
               (line non_hh birthyr if group==`x' & year==1960, lcolor(purple) lpat(solid) lwidth(0.4)) 
               (line non_hh birthyr if group==`x' & year==1970, lcolor(lavender) lpat(dash_dot) lwidth(0.4)) 
               (line non_hh birthyr if group==`x' & year==1980, lcolor(pink) lpat(longdash) lwidth(0.4)) 
               (line non_hh birthyr if group==`x' & year==1990, lcolor(cranberry) lpat(solid) lwidth(0.4)) 
               (line non_hh birthyr if group==`x' & year==2000, lcolor(orange_red) lpat(dash) lwidth(0.4)) 
               (line non_hh birthyr if group==`x' & year==2010, lcolor(orange*0.7) lpat(longdash) lwidth(0.4))     
        , 
            legend(on order(1 "1950" 2 "1960" 3 "1970" 4 "1980" 5 "1990" 6 "2000" 7 "2010") rows(2))
            ytitle("Share in institutions" " ") xtitle(" " "Birth year")
            ylabel(0(4)16, axis(1)) xlabel(1910(10)1980) xscale(r(1910 1980)) ;
        #delimit cr 
    graph export "$Mydirectory2/appendix_a/Census_1950to2010_shareGQ_`title'_byyear.pdf", as(pdf) replace
    }
    
    